﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace System.Helper
{
    /// <summary>
    /// log4net泛型扩展辅助类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Log4netHelper<T> where T : new()
    {
        private Log4netHelper() { }
        #region Debug
        /// <summary>
        /// 使用log4net Debug级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(object message)
        {
            LogManager.GetLogger(typeof(T)).Debug(message);
        }

        /// <summary>
        /// 使用log4net Debug级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Debug(object message, Exception exception)
        {
            LogManager.GetLogger(typeof(T)).Debug(message, exception);
        }

        /// <summary>
        /// 使用log4net Debug级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void DebugFormat(string format, params object[] args)
        {
            LogManager.GetLogger(typeof(T)).DebugFormat(format, args);
        }


        #endregion

        #region Info
        /// <summary>
        /// 使用log4net Info级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public static void Info(object message)
        {
            LogManager.GetLogger(typeof(T)).Info(message);
        }

        /// <summary>
        /// 使用log4net Info级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Info(object message, Exception exception)
        {
            LogManager.GetLogger(typeof(T)).Info(message, exception);
        }

        /// <summary>
        /// 使用log4net Info级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void InfoFormat(string format, params object[] args)
        {
            LogManager.GetLogger(typeof(T)).InfoFormat(format, args);
        }
        #endregion

        #region Warn
        /// <summary>
        /// 使用log4net Warn级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public static void Warn(object message)
        {
            LogManager.GetLogger(typeof(T)).Warn(message);
        }

        /// <summary>
        /// 使用log4net Warn级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Warn(object message, Exception exception)
        {
            LogManager.GetLogger(typeof(T)).Warn(message, exception);
        }

        /// <summary>
        /// 使用log4net Warn级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void WarnFormat(string format, params object[] args)
        {
            LogManager.GetLogger(typeof(T)).WarnFormat(format, args);
        }
        #endregion

        #region Error

        /// <summary>
        /// 使用log4net Error级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public static void Error(object message)
        {
            LogManager.GetLogger(typeof(T)).Error(message);
        }

        /// <summary>
        /// 使用log4net Error级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Error(object message, Exception exception)
        {
            LogManager.GetLogger(typeof(T)).Error(message, exception);
        }

        /// <summary>
        /// 使用log4net Error级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void ErrorFormat(string format, params object[] args)
        {
            LogManager.GetLogger(typeof(T)).ErrorFormat(format, args);
        }
        #endregion

        #region Fatal

        /// <summary>
        /// 使用log4net Fatal级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public static void Fatal(object message)
        {
            LogManager.GetLogger(typeof(T)).Fatal(message);
        }

        /// <summary>
        /// 使用log4net Fatal级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Fatal(object message, Exception exception)
        {
            LogManager.GetLogger(typeof(T)).Fatal(message, exception);
        }

        /// <summary>
        /// 使用log4net Fatal级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void FatalFormat(string format, params object[] args)
        {
            LogManager.GetLogger(typeof(T)).FatalFormat(format, args);
        }
        #endregion

        /// <summary>
        /// 返回Logger名称
        /// </summary>
        /// <returns></returns>
        public static string GetLoggerName()
        {
            return LogManager.GetLogger(typeof(T)).Logger.Name;
        }
    }

    /// <summary>
    /// log4net实例辅助类
    /// </summary>
    public class Log4netHelper
    {
        private ILog _log4net;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        public Log4netHelper(Type type)
        {
            _log4net = log4net.LogManager.GetLogger(type);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public Log4netHelper(string name)
        {
            _log4net = log4net.LogManager.GetLogger(name);
        }

        #region Debug
        /// <summary>
        /// 使用log4net Debug级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public void Debug(object message)
        {
            _log4net.Debug(message);
        }

        /// <summary>
        /// 使用log4net Debug级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public void Debug(object message, Exception exception)
        {
            _log4net.Debug(message, exception);
        }

        /// <summary>
        /// 使用log4net Debug级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void DebugFormat(string format, params object[] args)
        {
            _log4net.DebugFormat(format, args);
        }
        #endregion

        #region Info

        /// <summary>
        /// 使用log4net Info级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public void Info(object message)
        {
            _log4net.Info(message);
        }

        /// <summary>
        /// 使用log4net Info级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public void Info(object message, Exception exception)
        {
            _log4net.Info(message, exception);
        }

        /// <summary>
        /// 使用log4net Info级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void InfoFormat(string format, params object[] args)
        {
            _log4net.InfoFormat(format, args);
        }
        #endregion

        #region Warn
        /// <summary>
        /// 使用log4net Warn级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public void Warn(object message)
        {
            _log4net.Warn(message);
        }

        /// <summary>
        /// 使用log4net Warn级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public void Warn(object message, Exception exception)
        {
            _log4net.Warn(message, exception);
        }

        /// <summary>
        /// 使用log4net Warn级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void WarnFormat(string format, params object[] args)
        {
            _log4net.WarnFormat(format, args);
        }
        #endregion

        #region Error

        /// <summary>
        /// 使用log4net Error级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public void Error(object message)
        {
            _log4net.Error(message);
        }

        /// <summary>
        /// 使用log4net Error级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public void Error(object message, Exception exception)
        {
            _log4net.Error(message, exception);
        }

        /// <summary>
        /// 使用log4net Error级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void ErrorFormat(string format, params object[] args)
        {
            _log4net.ErrorFormat(format, args);
        }
        #endregion

        #region Fatal

        /// <summary>
        /// 使用log4net Fatal级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        public void Fatal(object message)
        {
            _log4net.Fatal(message);
        }

        /// <summary>
        /// 使用log4net Fatal级别记录日志信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public void Fatal(object message, Exception exception)
        {
            _log4net.Fatal(message, exception);
        }

        /// <summary>
        /// 使用log4net Fatal级别记录日志信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void FatalFormat(string format, params object[] args)
        {
            _log4net.FatalFormat(format, args);
        }
        #endregion

        /// <summary>
        /// 返回Logger名称
        /// </summary>
        /// <returns></returns>
        public string GetLoggerName()
        {
            return _log4net.Logger.Name;
        }
    }
}
